<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    @register="register"
    @ok="handleSubmit"
    width="40%"
    :okAuth="'m8:order:flow:over:edit'"
    @cancel="closeFunc"
  >
    <template #title>
      <span>
        补领料登记 【{{
          getTitle.title
        }}】
      </span>
    </template>
    <BasicForm @register="registerForm" v-bind="$attrs" :canFullscreen="false" />

  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'WorkCalendarModal',
  });
</script>
<script setup lang="ts">
  import { defineComponent, ref,computed } from 'vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { officeTreeData } from '/@/api/sys/office';
  import { moreForm, save } from '/@/api/m8/more/more';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';

  const emit = defineEmits(['success', 'register']);
  const { showMessage } = useMessage();
  let record = ref<any>();
    const getTitle = computed(() => ({
    title: '',
  }));
  const [register, { closeDrawer, setDrawerProps }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await moreForm(data);
    record.value = res.m8OrderPicFlowMore || {};
    record.value.__t = new Date().getTime();
    getTitle.value.title =
      record.value.orderPic.picno +
      '|' +
      record.value.orderPic.picname +
      '|' +
      record.value.oper.operName;
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    await validate();
    // openDrawer(true, '');
    handSave('1');
  }
  async function closeFunc() {
    handSave('0');
  }

  async function handSave(type) {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        id: record.value.id,
      };
      data.boverOper = type;
      const res = await save(params,data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', '');
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage('您填写的信息有误，请根据提示修正。');
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }

  const inputFormSchemas: FormSchema[] = [
    {
      label: '工序',
      field: 'oper.operCode',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
      required: true,
      show: false,
    },
    {
      label: '图纸',
      field: 'orderPic.id',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
      required: true,
      show: false,
    },
    {
      label: '流程',
      field: 'orderFlow.id',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
      required: true,
      show: false,
    },
    {
      label: '订单',
      field: 'order.id',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
      required: true,
      show: false,
    },
    {
      label: '补料人',
      field: 'createBy',
      fieldLabel: 'createName',
      component: 'TreeSelect',
      required: true,
      componentProps: {
        api: officeTreeData,
        params: { isLoadUser: true, userIdPrefix: '', isAll: true },
        canSelectParent: false,
        immediate: true,
        allowClear: true,
      },
    },
    {
      label: '下料人',
      field: 'manCodes',
      fieldLabel: 'manNames',
      component: 'TreeSelect',
      required: true,
      componentProps: {
        api: officeTreeData,
        params: { isLoadUser: true, userIdPrefix: '', isAll: true },
        canSelectParent: false,
        immediate: true,
        allowClear: true,
      },
    },
    {
      label: '补领数',
      field: 'iqty',
      component: 'Input',
      required: true,
    },
    {
      label: '材料领料说明',
      field: 'remarks',
      component: 'InputTextArea',
      required: true,
      componentProps: {
        maxlength: 500,
        rows: 10,
      },
      colProps: { lg: 24, md: 24 },
    },
  ];
  const [registerForm, { resetFields, setFieldsValue, validate, setProps }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 24, md: 24 },
  });
</script>
<style scoped>
  .ant-picker {
    width: 80%;
  }
</style>
